﻿using System;
using System.Collections.Generic;
using Assets.Scripts.Data;
using Assets.Scripts.UI;
using Assets.Scripts.Enums;
using Test30.Engine;
using UnityEngine;
using UnityEngine.UI;
using Object = UnityEngine.Object;

namespace Assets.Scripts.App
{
    public class PlayGameScreen : Control
    {
        public Game Game { get; private set; }
        public Dictionary<string,GameObject> Stats { get; set; }
        public PlayGameScreen() : base(UiType.Normal, UiMode.HideOther, Configs.UiPrefabsPath + Configs.PathConact + Configs.UiPrefabsNames.PlayGameScreen)
        {
        }

        public override void Init()
        {
            Game = Data as Game;
            if (Game != null) _hero = Game.Hero;
            InitScreen();
            ProcessGame();
        }

        public void ProcessGame()
        {
            GameResult result = new GameResult();
            while (!result.NeedsAction && !result.IsGameOver)
            {
                result = Game.Process();
                ObjectPoolComp.Get.ObjectPool.Update();
                if (result.NeedsPause) Ui.Delay();
                if (result.CheckForCancel) Game.CanelAction();
            }
        }

        private void InitScreen()
        {
            var trans = GameObj.transform;
            //底部属性栏
            var bottom = trans.Find(Configs.PlayGameScreen["Property"]);
            var go = trans.Find(Configs.PlayGameScreen["Parent"]).gameObject;
            foreach (EntityProperty value in Enum.GetValues(typeof(EntityProperty)))
            {
                var instance =Object.Instantiate<GameObject>(go);
                instance.transform.Find(Configs.PlayGameScreen["Title"]).GetComponent<Text>().text =
                    Configs.Space + value.ToString().Substring(0, 3) + Configs.Colon;
                instance.transform.SetParent(bottom);
                instance.transform.localScale = Vector3.one;
                instance.SetActive(true);
            }
            Object.Destroy(go);
            //底部经验条


            //顶部名字
            SetText(trans, Configs.PlayGameScreen["Name"], _hero.Name);
            string healthValue = $"{_hero.Health.Current}/{_hero.Health.Max}";
            SetText(trans, Configs.PlayGameScreen["HealthValue"], healthValue);


        }

        private void SetText(Transform trans,string title,string value)
        {
            trans.Find(title).GetComponent<Text>().text = value;
        }
        private Hero _hero;
    }
}
